package com.usercar.controller;


import com.dk.bean.Result;
import com.dk.em.ResultEnum;
import com.usercar.pojo.AdvertisingPrice;
import com.usercar.pojo.Content;
import com.usercar.pojo.Festival;
import com.usercar.service.IAdvertisingPriceService;
import com.usercar.service.IContentService;
import com.usercar.service.IFestivalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-08-11
 */
@RestController
@RequestMapping("/content")
@Api("广告统一接口api")
public class ContentController {

    @Autowired
    private IContentService iContentService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IFestivalService iFestivalService;
    @Autowired
    private IAdvertisingPriceService iAdvertisingPriceService;

    @ApiOperation("订单表的增加")
    @PostMapping
    public Result insert(@RequestBody Content content){
        iContentService.insertInfo(content);
        return Result.buildResult(ResultEnum.SUCCESS);
    }

    @ApiOperation("根据栏位判断时间是否存在")
    @GetMapping("/{field}/{startTime}/{endTime}")
    public Result selectTime(@PathVariable("field")Long field,@PathVariable("startTime") String startTime, @PathVariable("endTime") String endTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse(startTime);
        Date parse1 = sdf.parse(endTime);
        List<Content> list = iContentService.selectByFeild(field,parse,parse1);
        if(list.size() ==0){
            return Result.buildResult(ResultEnum.SUCCESS);
        }else {
            return Result.buildResult(ResultEnum.ERROR);
        }
    }

    @ApiOperation("根据栏位及是否包含节假日计算金额")
    @PostMapping("/select")
    public Result toInsert(@RequestBody Content content){
        Integer i = content.getIncludingHolidays();
        if(i == 1){//包含
            Calendar instance = Calendar.getInstance();
            int i1 = instance.get(Calendar.YEAR);
            String year = String.valueOf(i1);
            List<Festival> festivals = iFestivalService.selectByYear(year);
            int num =0;
            for (Festival festival : festivals) {
                Date time = festival.getTime();
                Content c = iContentService.selectByYear(time);
                if(c != null){
                    num +=1;
                }
            }
            AdvertisingPrice includeing =  iAdvertisingPriceService.selectByFieldAndIsFe(content.getField(),i);
            AdvertisingPrice notIncluding = iAdvertisingPriceService.selectByFieldAndIsFe(content.getField(),0);
            BigDecimal price = includeing.getPrice();
            String num1 = String.valueOf(num);
            BigDecimal b = new BigDecimal(num1);
            BigDecimal multiply = price.multiply(b);//节假日价格
            int ii = festivals.size()-num;
            String s = String.valueOf(ii);
            BigDecimal price1 = notIncluding.getPrice();
            BigDecimal a = new BigDecimal(s);
            BigDecimal multiply1 = price1.multiply(a);//节假日价格
            BigDecimal sum = multiply.add(multiply1);
            return Result.buildResult(ResultEnum.SUCCESS).setData(sum);
        }else {
            Date startTime = content.getStartTime();
            Date endTime = content.getEndTime();
            Calendar instance = Calendar.getInstance();
            int i1 = instance.get(Calendar.YEAR);
            String year = String.valueOf(i1);
            List<Festival> festivals = iFestivalService.selectByYear(year);
            int num =0;
            for (Festival festival : festivals) {
                Date time = festival.getTime();
                Content c = iContentService.selectByYear(time);
                if(c != null){
                    num +=1;
                }
            }
            int day = festivals.size()-num;
            AdvertisingPrice notIncluding = iAdvertisingPriceService.selectByFieldAndIsFe(content.getField(),0);
            BigDecimal price = notIncluding.getPrice();
            String day1 = String.valueOf(day);
            BigDecimal b = new BigDecimal(day1);
            BigDecimal multiply = price.multiply(b);
            content.setStatus(0);
            iContentService.insertInfo(content);
            return Result.buildResult(ResultEnum.SUCCESS).setData(multiply);
        }
    }

    //根据栏位判断，判断当前图片的地址
    @ApiOperation("根据栏位查询当前广告信息")
    @GetMapping("/seletByFile/{feild}")
    public Result selectByFeild(@PathVariable ("feild")Long feild){
        Content o = (Content)redisTemplate.opsForValue().get(feild);
        if(o != null){
            return Result.buildResult(ResultEnum.SUCCESS).setData(o);
        }else {
            Date d = new Date();
            Content c =  iContentService.selectByNowTime(d);
            Date startTime = c.getStartTime();
            Date endTime = c.getEndTime();
            long time = endTime.getTime()-d.getTime();
            redisTemplate.opsForValue().set(feild,c);
            return  Result.buildResult(ResultEnum.SUCCESS).setData(c);
        }

    }

    @ApiOperation("根据订单号修改状态")
    @PutMapping("/{out_trade_no}")
    public Result updateByout_trade_no(@PathVariable("out_trade_no")String out_trade_no){
        iContentService.updateByout_trade_no(out_trade_no);
        return Result.buildResult(ResultEnum.SUCCESS);
    }

}

